#
# 状态机

import time

from .logger import logger
from .states import GameState, PatrolState, StateManager

from .actions import \
    adb_stop, adb_action_wait, adb_action_tap, adb_action_swipe, \
    adb_action_find_img, adb_action_until_find_img


POS_COMBAT_HEAD1 = (100, 650)
POS_COMBAT_HEAD2 = (270, 650)
POS_COMBAT_HEAD3 = (440, 650)
POS_COMBAT_HEAD4 = (610, 650)
POS_COMBAT_HEAD5 = (800, 650)
POS_COMBAT_HEAD6 = (970, 650)
POS_COMBAT_SUB = (1140, 650)

POS_COMBAT_SKILL0 = (150, 560)
POS_COMBAT_SKILL1 = (400, 560)
POS_COMBAT_SKILL2 = (640, 560)
POS_COMBAT_SKILL3 = (890, 560)
POS_COMBAT_SKILL4 = (1140, 560)

POS_COMBAT_ATTACK = (1170, 620)
POS_COMBAT_START = (780, 650)
POS_COMBAT_AF = (1200, 50)

POS_HS_1 = (200, 300)
POS_STAGE4 = (425, 278)
POS_BUTTON_CLOSE = (1270, 10)

POS_BUTTON_MAP = (280, 660)
POS_BUTTON_OUTLAND = (1200, 660)
POS_BUTTON_ANCIENT = (80, 110)
POS_BUTTON_CURRENT = (200, 110)
POS_BUTTON_FUTURE = (320, 110)
POS_BUTTON_CEER = (200, 360)
POS_BUTTON_MIGU = (670, 390)
POS_BUTTON_GARLU = (1100, 360)
# map menu2
POS_BUTTON_MAP1 = (970, 250)
POS_BUTTON_MAP2 = (970, 350)
POS_BUTTON_MAP3 = (970, 450)
# for equip
POS_BUTTON_TEAM6 = (1100, 620)
POS_BUTTON_WEAPON = (700, 660)
POS_BUTTON_EQUIP = (920, 660)
POS_BUTTON_BADGE1 = (270, 400)
POS_BUTTON_BADGE2 = (270, 530)
POS_BUTTON_EQUIP_CONFIRM = (270, 650)
# for revive
POS_BUTTON_EARTH = (320, 300)
POS_BUTTON_WATER = (320, 380)
POS_BUTTON_REVIVE = (280, 650)


APP_NAME = 'games.wfs.anothereden'


#----------------------------------------------------------------------
# 登录
class HKLoginState(GameState):

    _done_delay = 60

    def _enter(self):
        adb_action_until_find_img('./data/aed_icon_hk.png', final_click = True, delay = 5)
        adb_action_wait(15)
        adb_action_until_find_img('./data/aed_button_menu_hk.png', click_pos = POS_COMBAT_ATTACK, delay = 10)

hk_login_state = HKLoginState()


#----------------------------------------------------------------------
class HKPatrolState(PatrolState):

    _combat_img_filename = "./data/aed_combat_hk.png"


hk_patrol_state = HKPatrolState()


#----------------------------------------------------------------------
# 战斗状态
class HKCombatState(GameState):

    _done_delay = 6

    def _enter(self):
        for idx in range(6):
            adb_action_tap(POS_COMBAT_ATTACK)
            adb_action_wait(1)

hk_combat_state = HKCombatState()


hk_patrol_state.set_next_state1(hk_patrol_state)
hk_patrol_state.set_next_state2(hk_combat_state)
hk_combat_state.set_next_state1(hk_patrol_state)


#----------------------------------------------------------------------
# state 管理器
hk_state_mgr = StateManager(APP_NAME)
hk_state_mgr.init(hk_patrol_state, hk_patrol_state)


#----------------------------------------------------------------------
class HKToHTState(GameState):

    _done_delay = 15

    def _enter(self):
        res = adb_action_find_img("./data/aed_button_return_hk.png")
        if res:
            adb_action_tap(res)
            adb_action_wait(1)
            adb_action_tap(POS_BUTTON_CLOSE)
            adb_action_wait(3)
        adb_action_until_find_img('./data/aed_button_menu_hk.png')
        adb_action_tap(POS_BUTTON_MAP)
        adb_action_wait(3)
        adb_action_tap(POS_BUTTON_ANCIENT)
        adb_action_wait(2)
        adb_action_tap(POS_BUTTON_OUTLAND)
        adb_action_wait(2)
        adb_action_tap(POS_BUTTON_GARLU)
        adb_action_wait(2)
        adb_action_until_find_img('./data/aed_map_zx.png', final_click = True)
        adb_action_wait(2)
        adb_action_tap(POS_BUTTON_MAP2)
        adb_action_wait(1)
        adb_action_until_find_img('./data/aed_button_yes_hk.png', final_click = True)
        adb_action_wait(5)
        adb_action_swipe((640, 180), (640, 540), 1000)
        adb_action_wait(3)
        adb_action_swipe((1000, 320), (280, 320), 1400)
        adb_action_wait(1)

hk_to_ht_state = HKToHTState()


#----------------------------------------------------------------------
#返回
class HKToHSState(GameState):

    _done_delay = 15

    def _enter(self):
        adb_action_until_find_img("./data/aed_button_menu_hk.png")
        adb_action_tap(POS_BUTTON_MAP)
        adb_action_wait(3)
        adb_action_tap(POS_BUTTON_OUTLAND)
        adb_action_wait(2)
        adb_action_tap(POS_BUTTON_MIGU)
        adb_action_wait(2)
        adb_action_tap(POS_BUTTON_FUTURE)
        adb_action_wait(2)
        adb_action_until_find_img('./data/aed_map_iluzen.png', m_degree = 0.1, final_click = True)
        adb_action_wait(2)
        adb_action_tap(POS_BUTTON_MAP3)
        adb_action_wait(1)
        adb_action_until_find_img('./data/aed_button_yes_hk.png', final_click = True)
        adb_action_wait(7)

hk_to_hs_state = HKToHSState()


#----------------------------------------------------------------------
# 转生
class HKReviveState(GameState):

    _done_delay = 15

    def _enter(self):
        res = adb_action_until_find_img('./data/aed_button_revive.png', max_times = 3, log = True)
        if not res:
            self.switch()
            return
        adb_action_tap(res)
        adb_action_wait(1)
        # do revive
        adb_action_tap(POS_BUTTON_EARTH)
        adb_action_wait(1)
        adb_action_until_find_img('./data/aed_revive_iname_hk.png', final_click = True)
        adb_action_wait(1)
        adb_action_tap(POS_BUTTON_WATER)
        adb_action_wait(1)
        adb_action_until_find_img('./data/aed_revive_iname_hk.png', final_click = True)
        adb_action_wait(1)
        adb_action_tap(POS_BUTTON_REVIVE)
        adb_action_wait(1)
        adb_action_until_find_img('./data/aed_button_yes_hk.png', final_click = True)
        adb_action_wait(10)
        # end
        for idx in range(10):
            adb_action_tap(POS_COMBAT_ATTACK)
            adb_action_wait(2)
        adb_action_until_find_img('./data/aed_button_yes_hk.png', click_pos = (640, 300),
                                  final_click = True, delay = 2, max_times = 10)
        adb_action_wait(1)
        for idx in range(4):
            adb_action_tap(POS_COMBAT_ATTACK)
            adb_action_wait(1)


hk_revive_state = HKReviveState()


#----------------------------------------------------------------------
# 佩戴经验 50% up
class HKExpUpState(GameState):

    _done_delay = 5

    def _enter(self):
        pos = adb_action_until_find_img('./data/aed_button_menu_hk.png', final_click = True)
        adb_action_wait(2)
        adb_action_tap(pos)
        adb_action_wait(2)
        adb_action_tap(POS_BUTTON_TEAM6)
        adb_action_wait(1)
        adb_action_tap(POS_BUTTON_EQUIP)
        adb_action_wait(1)
        adb_action_tap(POS_BUTTON_BADGE1)
        adb_action_wait(1)
        # swipe down
        adb_action_swipe((960, 540), (960, 160), 1000)
        adb_action_wait(2)
        adb_action_swipe((960, 540), (960, 150), 1000)
        adb_action_wait(2)
        #
        target_found = False
        dy0 = 120
        dy1 = 360
        res = adb_action_until_find_img('./data/aed_badge_top_hk.png', max_times = 3, log = True)
        if res and res[1] <= 320:
            target_found = True
        else:
            dy0 = -360
            dy1 = -120
            res = adb_action_until_find_img('./data/aed_badge_bottom_hk.png', max_times = 3, log = True)
            if res and res[1] >= 500:
                target_found = True
        if not target_found:
            adb_action_tap(POS_BUTTON_CLOSE)
            adb_action_wait(1)
            self.switch()
            return
        # found
        adb_action_tap((res[0], res[1] + dy0))
        adb_action_wait(1)
        # badge2 not worked now
        adb_action_tap(POS_BUTTON_BADGE2)
        adb_action_wait(1)
        adb_action_tap((res[0], res[1] + dy1))
        adb_action_wait(1)
        adb_action_tap(POS_BUTTON_EQUIP_CONFIRM)
        adb_action_wait(1)
        adb_action_tap(POS_BUTTON_CLOSE)
        adb_action_wait(1)


hk_exp_up_state = HKExpUpState()


#----------------------------------------------------------------------
# 回生副本初始状态
class HKHSInitState(GameState):

    _done_delay = 5

    def _enter(self):
        adb_action_until_find_img('./data/aed_hs_start.png', final_click = True, log = True)
        adb_action_wait(2)
        adb_action_tap(POS_HS_1)
        adb_action_wait(2)

hk_hs_init_state = HKHSInitState()


#----------------------------------------------------------------------
# 回生副本 stage4
class HKHSPatrolState(GameState):

    _done_delay = 3

    def _enter(self):
        pos = adb_action_until_find_img("./data/aed_stage4.png",
                                        click_pos = POS_COMBAT_ATTACK, max_times = 5)
        if not pos:
            # 未找到，继续
            self.switch()
            return
        adb_action_tap(POS_STAGE4)
        adb_action_wait(2)
        if adb_action_find_img("./data/aed_head6_lv_max.png", m_degree = 0.01):
            # finished
            logger.info('lv max **********')
            self.switch3()
            return
        adb_action_tap(POS_COMBAT_START)
        adb_action_wait(10)
        # adb_action_until_find_img("./data/aed_combat_hk.png", delay = 1)

hs_patrol_state = HKHSPatrolState()


#----------------------------------------------------------------------
# 回生副本第一关
class HKHSCombat1State(GameState):

    _done_delay = 15

    def _enter(self):
        # adb_action_tap(POS_COMBAT_AF)
        # adb_action_wait(0.1)
        # adb_action_tap(POS_COMBAT_SKILL0)
        # adb_action_wait(0.1)
        # adb_action_tap(POS_COMBAT_SKILL1)
        # adb_action_wait(0.1)
        # adb_action_tap(POS_COMBAT_SKILL1)
        # adb_action_wait(0.1)
        # adb_action_tap(POS_COMBAT_SKILL2)
        # for i in range(8):
        #     adb_action_wait(0.1)
        #     adb_action_tap(POS_COMBAT_SKILL3)
        # for i in range(8):
        #     adb_action_wait(0.1)
        #     adb_action_tap(POS_COMBAT_SKILL1)
        for idx in range(24):
            adb_action_tap(POS_COMBAT_ATTACK)
            adb_action_wait(1)
        # adb_action_until_find_img('./data/aed_combat_hk.png', delay = 2)

hs_combat1_state = HKHSCombat1State()


#----------------------------------------------------------------------
# 回生副本第二关
class HKHSCombat2State(GameState):

    _done_delay = 5

    def _enter(self):
        # adb_action_tap(POS_COMBAT_HEAD1)
        # adb_action_wait(0.3)
        # adb_action_tap(POS_COMBAT_SKILL3)
        # adb_action_wait(0.3)
        # adb_action_tap(POS_COMBAT_HEAD2)
        # adb_action_wait(0.3)
        # adb_action_tap(POS_COMBAT_SKILL3)
        # adb_action_wait(0.3)
        # adb_action_tap(POS_COMBAT_HEAD3)
        # adb_action_wait(0.3)
        # adb_action_tap(POS_COMBAT_SKILL3)
        # adb_action_wait(0.3)
        for idx in range(4):
            adb_action_tap(POS_COMBAT_ATTACK)
            adb_action_wait(1)
        adb_action_wait(3)
        # adb_action_until_find_img('./data/aed_combat_hk.png', delay = 2)

hs_combat2_state = HKHSCombat2State()


#----------------------------------------------------------------------
# 回生副本第三关
class HKHSCombat3State(GameState):

    _done_delay = 5

    def _enter(self):
        # adb_action_tap(POS_COMBAT_ATTACK)
        # adb_action_wait(9)
        for i in range(4):
            adb_action_tap(POS_COMBAT_ATTACK)
            adb_action_wait(1)


hs_combat3_state = HKHSCombat3State()

hk_login_state.set_next_state1(hk_hs_init_state)
hk_hs_init_state.set_next_state1(hs_patrol_state)
hs_patrol_state.set_next_state1(hs_combat1_state)
hs_patrol_state.set_next_state2(hk_login_state)
hs_patrol_state.set_next_state3(hk_to_ht_state)
hs_combat1_state.set_next_state1(hs_patrol_state)
# for revive
hk_to_ht_state.set_next_state1(hk_revive_state)
hk_revive_state.set_next_state1(hk_exp_up_state)
hk_revive_state.set_next_state2(hk_to_ht_state)
hk_exp_up_state.set_next_state1(hk_to_hs_state)
hk_exp_up_state.set_next_state2(hk_exp_up_state)
hk_to_hs_state.set_next_state1(hk_hs_init_state)


# not used
hs_combat2_state.set_next_state1(hs_combat3_state)
hs_combat3_state.set_next_state1(hs_patrol_state)

hk_hs_state_mgr = StateManager(APP_NAME)
hk_hs_state_mgr.init(hk_login_state, hk_hs_init_state)
# for test
# hk_hs_state_mgr.init(hk_login_state, hk_exp_up_state)
